package lv.bond.science.nnstudio.ui.testgraph;

import java.awt.Dimension;
import java.awt.Font;
import java.util.Iterator;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class TestGraphView extends ViewPart {

	public static final String ID = 
		"lv.bond.science.nnstudio.ui.errorgraph.TestGraphView"; 
	// Needs to be whatever is mentioned in plugin.xml

	private Composite top = null;
	private Composite composite = null;
	private Button button = null;
	
	private float[] testData = {0.2f, 0.5f, 0.4f};
	
	private static DefaultCategoryDataset defaultcategoryDataset = 
		new DefaultCategoryDataset();  //  @jve:decl-index=0:

	@Override
	public void createPartControl(Composite parent) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		top = new Composite(parent, SWT.NONE);
		top.setLayout(gridLayout);
		createComposite();
		button = new Button(top, SWT.NONE);
		
		button.setText("Run");
		button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				//fillDataset();
				//appendToDataSet(testData);
				//ImagesBean beanWithPaths = ImagesBean.getInstance();
				//System.out.println("path-"+beanWithPaths.getFileNameTestData()+";");
			}
		});
	}

	@Override
	public void setFocus() {
		composite.setFocus();
	}

	/**
	 * This method initializes composite	
	 *
	 */
	private void createComposite() {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = GridData.FILL;
		composite = new Composite(top, SWT.EMBEDDED);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(gridData);

		embedJPanelInto(composite);
	    
	}
	
	private void embedJPanelInto(Composite container) {
		java.awt.Frame graphFrame = 
	    	SWT_AWT.new_Frame(container);
	    java.awt.Panel panel = 
	    	new java.awt.Panel(new java.awt.BorderLayout());
	    graphFrame.add(panel);
	    JScrollPane scrollPane = new JScrollPane(
	    		TestNetwork("Normal Distribution Demo"));
	    panel.add(scrollPane);
		
	}

	public JPanel TestNetwork(String s) {
        JPanel jpanel = errorGraphPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        
        return jpanel;
    }
    
    public static void clearDataset() {
    	System.out.println("clearDataset for TestNet doesn`t mplemented yet!");
    }
    
    public static void appendToDataSet(double[] data, int key) {
    	for (int i = 0; i < data.length; i++) {
			TestGraphView
				.defaultcategoryDataset
				.addValue(data[i], "Network Error", "Test "+key);
		}
    }
    
    public JPanel errorGraphPanel() {
    	CategoryDataset categoryDataset = createDataset();
        JFreeChart jfreechart = createChart(categoryDataset);
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setPreferredSize(new Dimension(360, 500));
        return chartpanel;
    }

    private static CategoryDataset createDataset() {
    	/*
    	defaultcategoryDataset.addValue(1D, "Network Traffic", "Monday");
    	defaultcategoryDataset.addValue(0.003D, "Network Traffic", "Tuesday");
    	defaultcategoryDataset.addValue(0.02D, "Network Traffic", "Wednesday");
    	defaultcategoryDataset.addValue(0.00012D, "Network Traffic", "Thursday");
    	defaultcategoryDataset.addValue(0.0003D, "Network Traffic", "Friday");
    	*/
    	return defaultcategoryDataset;
    }

    private static JFreeChart createChart(CategoryDataset categoryDataset) {
    	JFreeChart jfreechart = 
    		ChartFactory.createBarChart("Netwrok error on Tests", null, "NET Error", categoryDataset, PlotOrientation.VERTICAL, false, true, false);
        //JFreeChart jfreechart = ChartFactory.createXYLineChart(null, "Epochs", "Error", xydataset, PlotOrientation.VERTICAL, true, true, false);
        //TextTitle texttitle = new TextTitle("Network test", new Font("SansSerif", 1, 14));
        //jfreechart.addSubtitle(texttitle);
        //XYPlot xyplot = jfreechart.getXYPlot();
        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
        categoryplot.setDomainGridlinesVisible(true);
        categoryplot.setRangeGridlinesVisible(true);
        categoryplot.setNoDataMessage("NO DATA!");
        NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
        numberaxis.setUpperMargin(0.12D);
        numberaxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
        CategoryAxis numberaxis1 = (CategoryAxis)categoryplot.getDomainAxis();
        //numberaxis1.
        //numberaxis1.setAutoRangeIncludesZero(false);

        return jfreechart;
    }

}
